From 5a9847b87c87eaab9412cf4e4b8e0799ebbffb04 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Wed, 3 Aug 2022 07:54:29 -0600 Subject: [PATCH] convert html writer to textstream. (#904) --- html.cc | 153 +++++++++--------- html.h | 12 +- reference/gc/GC7FA4.html | 6 +- ...A8~text-encoded.txt => GCGCA8-encoded.txt} | 0 reference/gc/{GCGCA8~html.txt => GCGCA8.html} | 6 +- reference/gc/{GCGCA8~text.txt => GCGCA8.txt} | 0 ...{GCGCA8_logs~html.txt => GCGCA8_logs.html} | 6 +- testo.d/text.test | 8 +- 8 files changed, 103 insertions(+), 88 deletions(-) rename reference/gc/{GCGCA8~text-encoded.txt => GCGCA8-encoded.txt} (100%) rename reference/gc/{GCGCA8~html.txt => GCGCA8.html} (98%) rename reference/gc/{GCGCA8~text.txt => GCGCA8.txt} (100%) rename reference/gc/{GCGCA8_logs~html.txt => GCGCA8_logs.html} (99%) diff --git a/html.cc b/html.cc index 254009524..4619040ad 100644 --- a/html.cc +++ b/html.cc @@ -21,17 +21,20 @@ #include "html.h" +#include // for QChar +#include // for QIODevice, QIODevice::WriteOnly #include // for QString, operator!= +#include // for QTextStream #include // for CaseInsensitive #include // for int32_t -#include // for localtime, time_t, tm +#include // for localtime, time_t -#include "defs.h" // for Waypoint, xfree, geocache_data, html_entitize, CSTR, pretty_deg_format, rot13, strip_nastyhtml, waypt_disp_all, utf_string, METERS_TO_FEET, gpsbabel_testmode, gs_get_cachetype, gs_get_container, mkshort_del_handle, mkshort_from_wpt, mkshort_new_handle +#include "defs.h" #include "formspec.h" // for FormatSpecificDataList, kFsGpx -#include "gbfile.h" // for gbfprintf, gbfclose, gbfopen, gbfputs #include "jeeps/gpsmath.h" // for GPS_Math_WGS84_To_UTM_EN #include "src/core/datetime.h" // for DateTime +#include "src/core/textstream.h" // for TextStream #include "src/core/xmltag.h" // for xml_findfirst, xml_tag, xml_attribute, fs_xml, xml_findnext @@ -40,14 +43,17 @@ void HtmlFormat::wr_init(const QString& fname) { - file_out = gbfopen(fname, "w", MYNAME); + file_out = new gpsbabel::TextStream; + file_out->open(fname, QIODevice::WriteOnly, MYNAME); mkshort_handle = mkshort_new_handle(); } void HtmlFormat::wr_deinit() { - gbfclose(file_out); + file_out->close(); + delete file_out; + file_out = nullptr; mkshort_del_handle(&mkshort_handle); } @@ -63,53 +69,55 @@ HtmlFormat::html_disp(const Waypoint* wpt) const GPS_Math_WGS84_To_UTM_EN(wpt->latitude, wpt->longitude, &utme, &utmn, &utmz, &utmzc); - gbfprintf(file_out, "\n
\n", CSTR(wpt->shortname)); - gbfprintf(file_out, "\n"); - gbfprintf(file_out, "\n"; + *file_out << "

%s - ",(global_opts.synthesize_shortnames) ? CSTR(mkshort_from_wpt(mkshort_handle, wpt)) : CSTR(wpt->shortname)); - gbfprintf(file_out, "%s (%d%c %6.0f %7.0f)", - CSTR(pretty_deg_format(wpt->latitude, wpt->longitude, degformat[2], " ", true)), - utmz, utmzc, utme, utmn); + *file_out << "\nshortname << "\">


\n"; + *file_out << "\n"; + QString sn = global_opts.synthesize_shortnames ? mkshort_from_wpt(mkshort_handle, wpt) : wpt->shortname; + *file_out << "\n"); + *file_out << "

\n"; - gbfprintf(file_out, "\n"); + *file_out << "\n"; - gbfprintf(file_out, "

" << sn << " - "; + *file_out << QStringLiteral("%1 (%2%3 %4 %5)") + .arg(pretty_deg_format(wpt->latitude, wpt->longitude, degformat[2], " ", true)) + .arg(utmz) + .arg(utmzc) + .arg(utme, 6, 'f', 0) + .arg(utmn, 7, 'f', 0); if (wpt->altitude != unknown_alt) { - gbfprintf(file_out, " alt:%d", (int)((altunits[0]=='f')?METERS_TO_FEET(wpt->altitude):wpt->altitude)); + *file_out << QStringLiteral(" alt:%1") + .arg((int)((altunits[0]=='f') ? METERS_TO_FEET(wpt->altitude) : wpt->altitude)); } - gbfprintf(file_out, "
\n"); + *file_out << "
\n"; if (wpt->description != wpt->shortname) { if (wpt->HasUrlLink()) { - gbfputs(QStringLiteral("%2") - .arg(wpt->GetUrlLink().url_, html_entitize(wpt->description)), - file_out); + *file_out << "GetUrlLink().url_ << "\">" + << html_entitize(wpt->description) << ""; } else { - gbfprintf(file_out, "%s", CSTR(wpt->description)); + *file_out << wpt->description; } if (!wpt->gc_data->placer.isEmpty()) { - gbfprintf(file_out, " by %s", CSTR(wpt->gc_data->placer)); + *file_out << " by " << wpt->gc_data->placer; } } - gbfprintf(file_out, "

"); + *file_out << ""; if (wpt->gc_data->terr) { - gbfprintf(file_out, "

%d%s / %d%s
\n", - (int)(wpt->gc_data->diff / 10), (wpt->gc_data->diff%10) ? "½" : "", - (int)(wpt->gc_data->terr / 10), (wpt->gc_data->terr%10) ? "½" : ""); - gbfputs(QStringLiteral("%1 / %2

") - .arg(gs_get_cachetype(wpt->gc_data->type), - gs_get_container(wpt->gc_data->container)), - file_out); + *file_out << QStringLiteral("

%1%2 / %3%4
\n") + .arg((int)(wpt->gc_data->diff / 10)) + .arg((wpt->gc_data->diff%10) ? "½" : "") + .arg((int)(wpt->gc_data->terr / 10)) + .arg((wpt->gc_data->terr%10) ? "½" : ""); + *file_out << gs_get_cachetype(wpt->gc_data->type) << " / " + << gs_get_container(wpt->gc_data->container) << "

"; } - gbfprintf(file_out, "
"); + *file_out << "
"; if (!wpt->gc_data->desc_short.utfstring.isEmpty()) { - gbfputs(QStringLiteral("

%1

\n") - .arg(strip_nastyhtml(wpt->gc_data->desc_short.utfstring)), - file_out); + *file_out << "

" + << strip_nastyhtml(wpt->gc_data->desc_short.utfstring) << "

\n"; } if (!wpt->gc_data->desc_long.utfstring.isEmpty()) { - gbfputs(QStringLiteral("

%1

\n") - .arg(strip_nastyhtml(wpt->gc_data->desc_long.utfstring)), - file_out); + *file_out << "

" + << strip_nastyhtml(wpt->gc_data->desc_long.utfstring) << "

\n"; } if (!wpt->gc_data->hint.isEmpty()) { QString hint; @@ -118,9 +126,10 @@ HtmlFormat::html_disp(const Waypoint* wpt) const } else { hint = wpt->gc_data->hint; } - gbfprintf(file_out, "

Hint: %s

\n", CSTR(hint)); + *file_out << "

Hint: " + << hint << "

\n"; } else if (!wpt->notes.isEmpty() && (wpt->description.isEmpty() || wpt->notes != wpt->description)) { - gbfprintf(file_out, "

%s

\n", CSTR(wpt->notes)); + *file_out << "

" << wpt->notes << "

\n"; } if (includelogs) { @@ -130,18 +139,18 @@ HtmlFormat::html_disp(const Waypoint* wpt) const xml_tag* curlog = xml_findfirst(root, "groundspeak:log"); while (curlog) { time_t logtime = 0; - gbfprintf(file_out, "

\n"); + *file_out << "

\n"; xml_tag* logpart = xml_findfirst(curlog, "groundspeak:type"); if (logpart) { - gbfprintf(file_out, "%s by ", CSTR(logpart->cdata)); + *file_out << "" + << logpart->cdata << " by "; } logpart = xml_findfirst(curlog, "groundspeak:finder"); if (logpart) { - gbfputs(QStringLiteral("%1 on ") - .arg(html_entitize(logpart->cdata)), - file_out); + *file_out << "" + << html_entitize(logpart->cdata) << " on "; } logpart = xml_findfirst(curlog, "groundspeak:date"); @@ -149,11 +158,10 @@ HtmlFormat::html_disp(const Waypoint* wpt) const logtime = xml_parse_time(logpart->cdata).toTime_t(); struct tm* logtm = localtime(&logtime); if (logtm) { - gbfprintf(file_out, - "%04d-%02d-%02d
\n", - logtm->tm_year+1900, - logtm->tm_mon+1, - logtm->tm_mday); + *file_out << QStringLiteral("%1-%2-%3
\n") + .arg(logtm->tm_year+1900, 4, 10, QChar('0')) + .arg(logtm->tm_mon+1, 2, 10, QChar('0')) + .arg(logtm->tm_mday, 2, 10, QChar('0')); } } @@ -161,9 +169,8 @@ HtmlFormat::html_disp(const Waypoint* wpt) const if (logpart) { double lat = xml_attribute(logpart->attributes, "lat").toDouble(); double lon = xml_attribute(logpart->attributes, "lon").toDouble(); - gbfprintf(file_out, - "%s
\n", - CSTR(pretty_deg_format(lat, lon, degformat[2], " ", true))); + *file_out << "" + << pretty_deg_format(lat, lon, degformat[2], " ", true) << "
\n"; } logpart = xml_findfirst(curlog, "groundspeak:text"); @@ -178,23 +185,23 @@ HtmlFormat::html_disp(const Waypoint* wpt) const s = logpart->cdata; } - gbfputs(html_entitize(s), file_out); + *file_out << html_entitize(s); } - gbfprintf(file_out, "

\n"); + *file_out << "

\n"; curlog = xml_findnext(root, curlog, "groundspeak:log"); } } } - gbfprintf(file_out, "
\n"); + *file_out << "
\n"; } void HtmlFormat::html_index(const Waypoint* wpt) const { - gbfputs(QStringLiteral("%1 - %2
\n") - .arg(html_entitize(wpt->shortname), html_entitize(wpt->description)), - file_out); + *file_out << QStringLiteral("%1 - %2
\n") + .arg(html_entitize(wpt->shortname), html_entitize(wpt->description)); } void @@ -202,42 +209,44 @@ HtmlFormat::write() { setshort_length(mkshort_handle, 6); - gbfprintf(file_out, "\n"); - gbfprintf(file_out, "\n"); - gbfprintf(file_out, "\n"); - gbfprintf(file_out, " \n"); + *file_out << "\n"; + *file_out << "\n"; + *file_out << "\n"; + *file_out << " \n"; // Don't write this line when running test suite. Actually, we should // probably not write this line at all... if (!gpsbabel_testmode()) { - gbfprintf(file_out, " \n", gpsbabel_version); + *file_out << " \n"; } - gbfprintf(file_out, " GPSBabel HTML Output\n"); + *file_out << " GPSBabel HTML Output\n"; if (stylesheet) { - gbfprintf(file_out, " \n", stylesheet); + *file_out << " \n"; } else { - gbfprintf(file_out, " \n"); + *file_out << " \n"; } - gbfprintf(file_out, "\n"); - gbfprintf(file_out, "\n"); + *file_out << "\n"; + *file_out << "\n"; - gbfprintf(file_out, "

\n"); + *file_out << "

\n"; auto html_index_lambda = [this](const Waypoint* waypointp)->void { html_index(waypointp); }; waypt_disp_all(html_index_lambda); - gbfprintf(file_out, "

\n"); + *file_out << "

\n"; auto html_disp_lambda = [this](const Waypoint* waypointp)->void { html_disp(waypointp); }; waypt_disp_all(html_disp_lambda); - gbfprintf(file_out, ""); - gbfprintf(file_out, ""); + *file_out << "\n"; + *file_out << "\n"; } diff --git a/html.h b/html.h index 0c74dca65..d80623241 100644 --- a/html.h +++ b/html.h @@ -21,12 +21,12 @@ #ifndef HTML_H_INCLUDED_ #define HTML_H_INCLUDED_ -#include // for QString -#include // for QVector +#include // for QString +#include // for QVector -#include "defs.h" // for arglist_t, ff_cap, ARG_NOMINMAX, ARGTYPE_STRING, ARGTYPE_BOOL, Waypoint, ff_cap_none, CET_CHARSET_UTF8, ff_cap_write, ff_type, ff_type_file, short_handle -#include "format.h" // for Format -#include "gbfile.h" // for gbfile +#include "defs.h" +#include "format.h" // for Format +#include "src/core/textstream.h" // for TextStream class HtmlFormat : public Format @@ -70,7 +70,7 @@ private: /* Data Members */ - gbfile* file_out{}; + gpsbabel::TextStream* file_out{nullptr}; short_handle mkshort_handle{}; char* stylesheet = nullptr; diff --git a/reference/gc/GC7FA4.html b/reference/gc/GC7FA4.html index 7c3987d03..bf2d18466 100644 --- a/reference/gc/GC7FA4.html +++ b/reference/gc/GC7FA4.html @@ -110,5 +110,7 @@ Point Geodesique situe near Port de Plaisance de Longueuil sur le bord du fleuve st-laurent. Il y a des sentiers et une grande piste cyclable Enjoy !

- - \ No newline at end of file + + + + diff --git a/reference/gc/GCGCA8~text-encoded.txt b/reference/gc/GCGCA8-encoded.txt similarity index 100% rename from reference/gc/GCGCA8~text-encoded.txt rename to reference/gc/GCGCA8-encoded.txt diff --git a/reference/gc/GCGCA8~html.txt b/reference/gc/GCGCA8.html similarity index 98% rename from reference/gc/GCGCA8~html.txt rename to reference/gc/GCGCA8.html index 239c450d0..8f7bcadbc 100644 --- a/reference/gc/GCGCA8~html.txt +++ b/reference/gc/GCGCA8.html @@ -36,5 +36,7 @@ Now that it's intuitively obvious to even the most casual observer where the cac
Member of Middle Tennessee GeoCachers Club [www.mtgc.org]

Hint: There Is No Hint

- - + + + + diff --git a/reference/gc/GCGCA8~text.txt b/reference/gc/GCGCA8.txt similarity index 100% rename from reference/gc/GCGCA8~text.txt rename to reference/gc/GCGCA8.txt diff --git a/reference/gc/GCGCA8_logs~html.txt b/reference/gc/GCGCA8_logs.html similarity index 99% rename from reference/gc/GCGCA8_logs~html.txt rename to reference/gc/GCGCA8_logs.html index e5bac5a79..5f75d6975 100644 --- a/reference/gc/GCGCA8_logs~html.txt +++ b/reference/gc/GCGCA8_logs.html @@ -60,5 +60,7 @@ In the expectation that this cache will get some traffic in the next 48 hours, R

Write note by robertlipe on 2003-07-03
It won't earn him a smiley face, but I've confirmed that rickrich would have indeed sunk the battleship! Thanx for playing. You get a copy of the home game and some rice-a-roni...

- - \ No newline at end of file + + + + diff --git a/testo.d/text.test b/testo.d/text.test index b60fb4ea4..e89b52226 100644 --- a/testo.d/text.test +++ b/testo.d/text.test @@ -4,12 +4,12 @@ gpsbabel -i gpx -f ${REFERENCE}/gc/GCGCA8.gpx \ -o html -F ${TMPDIR}/gc.html \ -o text -F ${TMPDIR}/gc.txt \ -o text,encrypt -F ${TMPDIR}/gc-encoded.txt -compare ${REFERENCE}/gc/GCGCA8~text.txt ${TMPDIR}/gc.txt -compare ${REFERENCE}/gc/GCGCA8~text-encoded.txt ${TMPDIR}/gc-encoded.txt -compare ${REFERENCE}/gc/GCGCA8~html.txt ${TMPDIR}/gc.html +compare ${REFERENCE}/gc/GCGCA8.txt ${TMPDIR}/gc.txt +compare ${REFERENCE}/gc/GCGCA8-encoded.txt ${TMPDIR}/gc-encoded.txt +compare ${REFERENCE}/gc/GCGCA8.html ${TMPDIR}/gc.html gpsbabel -i gpx -f ${REFERENCE}/gc/GCGCA8.gpx -o html,logs -F ${TMPDIR}/gc_logs.html -compare ${REFERENCE}/gc/GCGCA8_logs~html.txt ${TMPDIR}/gc_logs.html +compare ${REFERENCE}/gc/GCGCA8_logs.html ${TMPDIR}/gc_logs.html gpsbabel -i gpx -f ${REFERENCE}/gc/GC7FA4.gpx \ -o html,logs -F ${TMPDIR}/GC7FA4.html \ -- 2.30.2